package com.aiyige.page.user.behavior;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;
import android.util.AttributeSet;
import android.view.View;

import com.aiyige.R;

/**
 * Created by zhuyakun on 2017/10/19.
 */

public class UserPageTitleBarBehavior extends CoordinatorLayout.Behavior<View> {
    int currentImageResId = R.drawable.personal_back;
    float nestedScrollDistance;
    View dependency;

    public UserPageTitleBarBehavior() {
    }

    public UserPageTitleBarBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        if (dependency.getId() == R.id.appbar) {
            this.dependency = dependency;
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onLayoutChild(CoordinatorLayout parent, View child, int layoutDirection) {
        nestedScrollDistance = dependency.getMeasuredHeight() - child.getResources().getDimension(R.dimen.user_page_title_bar_height) - child.getResources().getDimension(R.dimen.user_page_tab_height);
        return super.onLayoutChild(parent, child, layoutDirection);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float x = Math.abs(dependency.getTranslationY()) / nestedScrollDistance;
        float y = x;
        child.setAlpha((float) Math.pow(y, 3));
        return false;
    }
}
